/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/util/StringUtils.java 1040 2012-07-02 09:20:33Z lanzhouxue $
 * $Id: StringUtils.java 1040 2012-07-02 09:20:33Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 17:20:33 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1040 $
 */
package com.elancom.framework.commons.util;

import java.util.Map;

/**
 * @author elancom
 * @date Apr 15, 2012
 */
public abstract class StringUtils {
	public static final String BLANK = "";

	public static final char[] chars = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
			'v', 'w', 'x', 'y', 'x', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	public static String arrayToString(String[] stringArray) {
		return arrayToString(stringArray, ",");
	}

	public static String arrayToString(int[] intArray) {
		return arrayToString(intArray, ",");
	}

	public static int[] stringArrayToIntArray(String[] stringArray) {
		if (stringArray == null) {
			return null;
		}

		if (stringArray.length == 0) {
			return new int[0];
		}

		int[] intArray = new int[stringArray.length];
		for (int i = 0; i < stringArray.length; i++) {
			intArray[i] = Integer.valueOf(stringArray[i]);
		}

		return intArray;
	}

	public static String arrayToString(int[] intArray, String joinchar) {
		if (intArray == null || intArray.length == 0) {
			return "";
		}

		String[] stringArray = intArrayToStringArray(intArray);
		return arrayToString(stringArray, joinchar);
	}

	public static String[] intArrayToStringArray(int[] intArray) {
		if (intArray == null || intArray.length == 0) {
			return new String[] {};
		}

		String[] stringArray = new String[intArray.length];
		for (int i = 0; i < intArray.length; i++) {
			stringArray[i] = intArray[i] + "";
		}

		return stringArray;
	}

	public static String arrayToString(String[] stringArray, String joinchar) {
		if (stringArray == null || stringArray.length == 0) {
			return "";
		}

		if (joinchar == null) {
			joinchar = ",";
		}

		StringBuffer sb = new StringBuffer();
		for (String element : stringArray) {
			sb.append(element).append(joinchar);
		}

		sb.delete(sb.length() - joinchar.length(), sb.length());

		return sb.toString();
	}

	public static boolean eqauls(String str1, String str2) {
		return (str1 == str2 || (str1 != null && str1.equals(str2)));
	}

	public static int indexOf(String str, String idxStr) {
		if (str == null) {
			return -1;
		}
		return str.indexOf(idxStr);
	}

	public static boolean inStr(String source, String str, String separator) {
		boolean exist = false;

		if (source == null || str == null) {
			return exist;
		}

		if (separator != null) {
			String[] strArr = source.split(separator);
			for (String s : strArr) {
				if (s.equals(str)) {
					exist = true;
					break;
				}
			}
		} else {
			exist = (source.indexOf(str) != -1);
		}

		return exist;
	}

	public static boolean isAllBlank(Object... objs) {
		if (objs == null || objs.length == 0) {
			return true;
		}

		for (Object o : objs) {
			if (!isBlank(o)) {
				return false;
			}
		}

		return true;
	}

	public static boolean isBlank(String str) {
		return str == null || str.trim().length() == 0;
	}

	public static boolean isBlank(Object obj) {
		return obj == null || obj.toString().trim().length() == 0;
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}

	public static boolean isNotBlank(Object str) {
		return !isBlank(str);
	}

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	public static String join(String str, int times) {
		return join(str, null, times);
	}

	public static String join(String str, String split, int times) {
		if (times < 1 || str == null) {
			return "";
		}
		if (times == 1) {
			return str;
		}
		if (split == null) {
			split = ",";
		}

		String rsStr = null;

		if (times > 50 && str.length() > 100) {
			StringBuffer buffer = new StringBuffer();

			for (int i = 0; i < times; i++) {
				buffer.append(str).append(split);
			}

			buffer.delete(buffer.length() - split.length(), buffer.length());
			rsStr = buffer.toString();
		} else {
			rsStr = "";
			for (int i = 0; i < times; i++) {
				rsStr += str + split;
			}
			rsStr = rsStr.substring(0, rsStr.length() - split.length());
		}

		return rsStr;
	}

	public static int length(String str) {
		return str == null ? 0 : str.length();
	}

	public static String random(String startStr, int length) {
		int charsLen = chars.length;
		StringBuffer sb = new StringBuffer();

		if (startStr != null) {
			sb.append(startStr);
		}

		for (int i = 0; i < length; i++) {
			sb.append(chars[(int) (Math.random() * charsLen)]);
		}

		return sb.toString();
	}

	public static String random(int length) {
		return random(null, length);
	}

	public static String returnEmptyIfBlank(String str) {
		if (isBlank(str)) {
			return "";
		}
		return str;
	}

	public static String returnEmptyIfNull(Object object) {
		if (object == null) {
			return "";
		}
		return object.toString();
	}

	public static String returnNullIfBlank(String str) {
		if (isBlank(str)) {
			return null;
		}
		return str;
	}

	public static String returnNullIfEmpty(String str) {
		if (isEmpty(str)) {
			return null;
		}
		return str;
	}

	public static <K, T> String rtp(String rtxt, Map<K, T> map) {
		if (map != null && rtxt != null) {
			for (K k : map.keySet()) {
				String key = (String) k;
				String replacement = (String) map.get(key);
				rtxt = rtxt.replaceAll("\\$\\{" + key + "\\}", replacement != null ? replacement : "");
			}
		}

		return rtxt;
	}

	public static String subInvert(String str, int len) {
		if (StringUtils.isBlank(str)) {
			return "";
		}

		if (len >= str.length()) {
			return str;
		}

		return str.substring(str.length() - len, str.length());
	}

	@SuppressWarnings("unused")
	public static String toFirstLower(String str) {
		if (true) {
			return uncapitalize(str);
		}

		if (isBlank(str)) {
			return str;
		}

		char c = str.substring(0, 1).toCharArray()[0];
		if (c > 64 && c < 91) {
			c = (char) (c + 32);

			if (str.length() > 1) {
				str = c + str.substring(1, str.length());
			}

		}

		return str;
	}

	public static String toString(int i) {
		return i + "";// new Integer(i).toString();
	}

	public static String toString(Object obj) {
		return obj == null ? null : obj.toString();
	}

	public static String trim(String str) {
		if (str != null) {
			str = str.trim();
		}
		return str;
	}

	/**
	 * 
	 * @param obj
	 * @return
	 */
	public static String trim(Object obj) {
		if (obj == null) {
			return null;
		}
		return obj.toString().trim();
	}

	public static String wrap(String source, String wchar, String splitChar) {
		if (source == null || source.trim().length() == 0) {
			return "";
		}

		String[] sourceArray = source.split(splitChar);

		return wrap(sourceArray, wchar, splitChar);
	}

	public static String wrap(String[] source, String wchar, String joinchar) {
		if (source == null || source.length == 0) {
			return "";
		}

		StringBuffer sb = new StringBuffer();

		for (String element : source) {
			sb.append(wchar).append(element).append(wchar).append(",");
		}

		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	public static boolean startsWithIgnoreCase(String str, String prefix) {
		if (str == null || prefix == null) {
			return false;
		}
		if (str.startsWith(prefix)) {
			return true;
		}
		if (str.length() < prefix.length()) {
			return false;
		}
		String lcStr = str.substring(0, prefix.length()).toLowerCase();
		String lcPrefix = prefix.toLowerCase();
		return lcStr.equals(lcPrefix);
	}

	/**
	 * 把第一个字符转大写
	 * 
	 * @param str
	 * @return
	 */
	public static String capitalize(String str) {
		return changeFirstCharacterCase(str, true);
	}

	/**
	 * 把第一个字符转小写
	 * 
	 * @param str
	 * @return
	 */
	public static String uncapitalize(String str) {
		return changeFirstCharacterCase(str, false);
	}

	public static boolean isIntegerString(String str) {
		if (isBlank(str)) {
			return false;
		}

		try {
			Integer.valueOf(str);
		} catch (Exception e) {
			return false;
		}

		return true;
	}

	public static boolean isNumberString(String str) {
		if (isBlank(str)) {
			return false;
		}

		try {
			Double.valueOf(str);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	private static String changeFirstCharacterCase(String str, boolean capitalize) {
		if (isBlank(str)) {
			return str;
		}
		StringBuilder sb = new StringBuilder(str.length());
		if (capitalize) {
			sb.append(Character.toUpperCase(str.charAt(0)));
		} else {
			sb.append(Character.toLowerCase(str.charAt(0)));
		}
		sb.append(str.substring(1));
		return sb.toString();
	}
}
